package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.EmployeeInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class EmployeeInfoActivity extends HttpRequestActivity implements View.OnClickListener {

	private static final String TAG = "EmployeeInfoActivity";
	private ActionBar mActionBar = null;

	private Button mActionDeleteEmployee = null;

	private EmployeeInfo mEmployeeInfo = null;

	private TextView mEmployeeName = null;
	private TextView mWsjAccount = null;
	private TextView mEnterTime = null;
	private TextView mAddPerson = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.initHttpComponent(EmployeeInfoActivity.class);
		setContentView(R.layout.activity_employee_info);
		mEmployeeInfo = (EmployeeInfo) getIntent().getSerializableExtra("info");

		mActionDeleteEmployee = findViewById(R.id.btn_delete_employee_action);
		mActionDeleteEmployee.setOnClickListener(this);

		mEmployeeName = findViewById(R.id.tv_employee_name);
		mWsjAccount = findViewById(R.id.tv_wsj_account);
		mEnterTime = findViewById(R.id.tv_enter_time);
		if(mEmployeeInfo!= null){
			mEmployeeName.setText(mEmployeeInfo.getName());
			mWsjAccount.setText(mEmployeeInfo.getAccount());
			mEnterTime.setText(mEmployeeInfo.getCreate_time());
		}
		mAddPerson = findViewById(R.id.tv_add_person);
		mAddPerson.setText(UserInfoController.Instance().getUserInfo().getUsername());

		mActionBar = findViewById(R.id.actionBarEmployeeInfo);
		mActionBar.setOnBackClickListener( new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void executeDeleteEmployeeInfoAction(){
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("token", AuthController.Instance().getToken());

		if(mEmployeeInfo != null){
			sendHttpRequest(
					IHttpRequest.RequestMethod.DELETE_METHOD,
					RemoteAPI.REQUEST_V1_CHILD_ACCOUNT + "/" + mEmployeeInfo.getUid(),
					header,
					null,
					RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT);
		}
	}

	@Override
	protected void onHttpResponse(int actionId, String response) {
		try {
			JSONObject res = new JSONObject(response);
			Gson gson = new Gson();
			if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					finish();
				}
				else{
					ToastUtils.showLongToast(this, res.getString("message"));
				}
			}
			else{

			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onHttpErrorResponse(int actionId, String response) {
		if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
			noticeOffline();
		}
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btn_delete_employee_action){
			executeDeleteEmployeeInfoAction();
		}
	}
}